<?php

namespace App\Admin\Forms;

use App\Models\Posting;
use App\Models\UserPrize;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Grid;
use Dcat\Admin\Widgets\Form;
use Dcat\Admin\Traits\LazyWidget;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Log;


class UpdateStatus extends Form implements LazyRenderable
{
    use LazyWidget;

    public function handle(array $input)
    {
        // 获取外部传递参数
        $id = $this->payload['id'] ?? null;
        // 表单参数
        $audit_type = $input['audit_type'] ?? null;
        if (! $id) {
            return $this->response()->error('参数错误');
        }

        $post = Posting::query()->find($id);

        if (! $post) {
            return $this->response()->error('帖子不存在');
        }
        try {
            $params ='';
            $client = new Client(['base_uri' => 'https://m-h5.elhqyr.vip/api/user/adminAudit' ,'timeout'  => 5.0,]);
            $response = $client->post('', ['json' => ['id'=>$id,
                'status'=>$audit_type?0:1]]);
            $data = json_decode($response->getBody()->getContents());
            if ($data->code == 'error'){
                Log::info('发帖接口详情',[$params,$data]);
                return $this->response()->error('接口报错：'.$data->msg);
            }
//            $post->update(['audit_type' => $audit_type?1:2]);
            return $this->response()->refresh()->success('审核成功');
        }catch (\Exception $exception){
            return  $this->error($exception->getMessage());
        }





    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $model = Posting::query()->find($this->payload['id']);
        $this->text('title')->value($model->title)->disable();
        $this->textarea('context')->value($model->context)->disable();
        $this->switch('audit_type', '审核通过？')->value();
        $img = explode(',',$model->url);
        $str = '';
        foreach ($img as $k){
            $str .= '<img src="'.$k.'" width=200px; style = "margin:0 8px;">';
        }
        $this->html($str,'图片');

    }

    /**
     * The data of the form.
     *
     * @return array
     */

}
